package com.service.samp.web.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.multipart.MultipartFile;

import com.framework.bas.exception.WebException;
import com.framework.bas.param.GeneralParam;
import com.framework.bas.web.controller.JsonResponse;
import com.framework.bas.web.controller.PageSupport;
import com.framework.bas.web.provider.IPlmQueryProvider;
import com.service.samp.util.AppSampConstant;
import com.service.samp.vo.Pagetest;

/**
 * @author cuiWenming
 */
@Controller
@RequestMapping("/page/samp/")
public class SampController extends AbsSampController {

	private static final long serialVersionUID = 1L;
	private final Logger logger = Logger.getLogger(this.getClass());

	// 新增用户
	@RequestMapping("addUser")
	public String addUser(Model model, @RequestParam(value = "user", required = false) String user, HttpServletRequest request, HttpServletResponse response) throws WebException {
		GeneralParam param = GeneralParam.getCurrent();
		model.addAttribute("name", "org/addUser");
		model.addAttribute("sessionName", user);
		model.addAttribute("requestName", "fd");
		param.setAppService("09010101");
		runService(param, request);
		// mv.setViewName("org/addUser");
		return "org/addUser";
	}

	@RequestMapping("addPagesSample")
	public String addPagesSample(Model model, Pagetest pageTest, HttpServletRequest request, HttpServletResponse response) throws WebException {
		GeneralParam param = GeneralParam.getCurrent();
		param.setAppService("09010101");
		param.setValue(AppSampConstant.PAGE_TEST_OBJ, pageTest);
		runService(param, request);
		model.addAttribute("pageTest", pageTest);
		return "redirect:pagesSample.do";
	}

	@SuppressWarnings("unchecked")
	@RequestMapping("findPageList")
	public @ResponseBody
	Object findPageList(Model model) throws WebException {

		Map qparams = new HashMap();
		qparams.put("sidx", "gid");
		qparams.put("sord", "desc");
		PageSupport pageResult = loadPages("app_samp.pageTest", "app_samp.pageSize", qparams);
		
		logger.debug(pageResult);
		return new JsonResponse(pageResult);
	}

	@SuppressWarnings("unchecked")
	@RequestMapping("pagesSample")
	public String pagesSample(Model model) throws Exception {

		Map qparams = new HashMap();
		qparams.put("sidx", "gid");
		qparams.put("sord", "desc");
		PageSupport pageResult = loadPages("app_samp.pageTest", "app_samp.pageSize", qparams);
		// 获取servletContext示例
		WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getRequest().getSession().getServletContext());
		model.addAttribute("pageSupport", pageResult);
		return "samp/pagesSample";
	}

	@RequestMapping("updatePagesSample")
	public String updatePagesSample(Model model,GeneralParam param1, Pagetest pageTest, @RequestParam(value = "flag", required = false) String flag) throws Exception {
		// 加载数据到更新页
		if (AppSampConstant.FLAG_TOU.equals(flag)) {
			Pagetest pagetest = getPlmBasBS().queryById(Pagetest.class, pageTest.getId());
			model.addAttribute("pt", pagetest);
			model.addAttribute("flag", AppSampConstant.FLAG_UPD);
			return "samp/addPageTest";
		} else {
			// 更新
			GeneralParam param = GeneralParam.getCurrent();
			param.setAppService("09010102");
			param.setValue(AppSampConstant.PAGE_TEST_OBJ, pageTest);
			runService(param, getRequest());
			return "redirect:pagesSample.do";
		}

	}

	@RequestMapping("deletePagesSample")
	public String deletePagesSample(Model model, Pagetest pageTest) throws Exception {
		// 删除
		GeneralParam param = GeneralParam.getCurrent();
		param.setAppService("09010103");
		param.setValue(AppSampConstant.PAGE_TEST_OBJ, pageTest);
		runService(param, getRequest());
		return "redirect:pagesSample.do";
	}

	@RequestMapping(value = "testUpload", method = RequestMethod.POST)
	public String handleFormUpload(@RequestParam("name") String name, @RequestParam("file") MultipartFile file) throws Exception {
		String path = this.servletContext.getRealPath("/upload/");
		if (!file.isEmpty()) {
			file.transferTo(new File("c://upload/" + file.getOriginalFilename()));
			// store the bytes somewhere
			return "redirect:pagesSample.do";
		} else {
			return "redirect:pagesSample.do";
		}
	}

	@RequestMapping("testJson")
	public @ResponseBody
	Object testJson() {
		Pagetest pt = new Pagetest();
		pt.setColor("red");
		pt.setId("fds");
		pt.setWeight("34");
		return pt;
	}

}
